package bma.common.langutil.ai.stack;

import java.util.concurrent.TimeoutException;

import bma.common.langutil.concurrent.IFace4ProcessTimerTask;
import bma.common.langutil.concurrent.ProcessTimerTaskAbstract;

public class AIStackOne4Timer<RESP_TYPE> extends AIStackOne<RESP_TYPE> {

	protected IFace4ProcessTimerTask task;

	public AIStackOne4Timer(AIStack<RESP_TYPE> delegate) {
		super(delegate);
	}

	public AIStackOne4Timer(AIStack<RESP_TYPE> delegate,
			IFace4ProcessTimerTask task) {
		super(delegate);
		this.task = task;
	}

	public IFace4ProcessTimerTask getTask() {
		return task;
	}

	public void setTask(IFace4ProcessTimerTask task) {
		this.task = task;
	}

	public ProcessTimerTaskAbstract createTimeout(final String msg, long to) {
		ProcessTimerTaskAbstract r = new ProcessTimerTaskAbstract(
				System.currentTimeMillis() + to) {
			@Override
			public void run() {
				failure(new TimeoutException(msg));
			}
		};
		this.task = r;
		return r;
	}

	@Override
	protected boolean done() {
		if (super.done()) {
			return true;
		}
		if (task != null) {
			task.cancel();
		}
		return false;
	}
}
